#include "http/znx_http_request.h"
#include "http/znx_http_client.h"


void test_seal_response_header()
{
    znx_pool_t                  *pool;
    znx_http_request_t          *request;
    znx_http_response_t         *response;
    znx_http_header_t           *header;
    znx_buf_t                   *header_buf;
    znx_result_t                res;
    znx_http_conn_t             *hc;

    pool = znx_pool_create();
    hc = znx_pool_calloc(pool, sizeof(znx_http_conn_t));

    request = znx_http_request_create(pool, hc, ZNX_FALSE);
    response = request->response;
    request->http_version = ZNX_HTTP_VERSION_11;

    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    znx_http_header_name_init(&header->name, (u_char *)"AAAA", sizeof("AAAA") - 1);
    ZNX_STR_SET(&header->val, "aaaa");
    znx_http_headers_add(&response->headers, header);

    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    znx_http_header_name_init(&header->name, (u_char *)"BBBB", sizeof("BBBB") - 1);
    ZNX_STR_SET(&header->val, "bbbb");
    znx_http_headers_add(&response->headers, header);

    response->trailers = znx_http_trailers_create(request->pool);
    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    znx_http_header_name_init(&header->name, (u_char *)"XXXX", sizeof("XXXX") - 1);
    ZNX_STR_SET(&header->val, "123");
    znx_http_headers_add(&response->trailers->headers, header);
    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    znx_http_header_name_init(&header->name, (u_char *)"YYYY", sizeof("YYYY") - 1);
    ZNX_STR_SET(&header->val, "456");
    znx_http_headers_add(&response->trailers->headers, header);

    request->want_close = 1;

    res = znx_http_seal_response_header(request, &header_buf);
    if (res != ZNX_SUCCESS) {
        ZNX_DEF_LOG_FATAL(NULL, "expect SUCCESS, but got other");
    }

    znx_str_t           tmp;
    tmp.data = header_buf->pos;
    tmp.len = (size_t)(header_buf->end - header_buf->pos);
    ZNX_DEF_LOG_DEBUG(NULL, "########\n%v###########", &tmp);
    znx_buf_free(header_buf);

    znx_http_request_destroy(request);
    znx_pool_destroy(pool);
}


void test_seal_request_header()
{
    znx_pool_t              *pool;
    znx_http_request_t      *request;
    znx_http_header_t       *header;
    znx_buf_t               *header_buf;
    znx_result_t            res;
    znx_http_conn_t         *hc;

    pool = znx_pool_create();
    hc = znx_pool_calloc(pool, sizeof(znx_http_conn_t));

    request = znx_http_request_create(pool, hc, ZNX_TRUE);
    request->request_uri.data = (u_char *)"/1.txt?a=1&b=2";
    request->request_uri.len = sizeof("/1.txt?a=1&b=2") - 1;

    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    znx_http_header_name_init(&header->name, (u_char *)"Host", sizeof("Host") - 1);
    ZNX_STR_SET(&header->val, "www.aaa.com");
    znx_http_headers_add(&request->headers, header);

    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    znx_http_header_name_init(&header->name, (u_char *)"AAAA", sizeof("AAAA") - 1);
    ZNX_STR_SET(&header->val, "aaaa");
    znx_http_headers_add(&request->headers, header);

    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    znx_http_header_name_init(&header->name, (u_char *)"BBBB", sizeof("BBBB") - 1);
    ZNX_STR_SET(&header->val, "bbbb");
    znx_http_headers_add(&request->headers, header);

    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    header->name = znx_http_transfer_encoding_hdr_name;
    header->val.len = ZNX_HTTP_CHUNKED_LEN;
    header->val.data = (u_char *)ZNX_HTTP_CHUNKED;
    znx_http_headers_add(&request->headers, header);

    request->trailers = znx_http_trailers_create(request->pool);
    header = znx_pool_calloc(pool, sizeof(znx_http_header_t));
    znx_http_header_name_init(&header->name, (u_char *)"AAA", sizeof("AAA") - 1);
    ZNX_STR_SET(&header->val, "1");
    znx_http_headers_add(&request->trailers->headers, header);
    
    request->want_close = 1;

    res = znx_http_seal_request_header(request, &header_buf);
    if (res != ZNX_SUCCESS) {
        ZNX_DEF_LOG_FATAL(NULL, "expect SUCCESS, but got other");
    }

    znx_str_t           tmp;
    tmp.data = header_buf->pos;
    tmp.len = (size_t)(header_buf->end - header_buf->pos);
    ZNX_DEF_LOG_DEBUG(NULL, "########\n%v###########", &tmp);
    znx_buf_free(header_buf);

    znx_http_request_destroy(request);
    znx_pool_destroy(pool);
}


int main()
{
    test_seal_response_header();
    test_seal_request_header();

    return 0;
}